package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.core.BaseServlet;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.User;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.UserService;
import com.itheima.service.system.impl.DeptServiceImpl;
import com.itheima.service.system.impl.UserServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 平台系统管理  -> 用户管理(控制层)
 */
@WebServlet("/system/user/*")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();
    private DeptService deptService = new DeptServiceImpl();
    /**
     * 分页查询列表数据
     */
    public String list(HttpServletRequest request, HttpServletResponse response) throws Exception{
        /*获取表单数据*/
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        if(StringUtils.isBlank(pageNum)){
            pageNum="1";
        }
        if(StringUtils.isBlank(pageSize)){
            pageSize="5";
        }

        //调用Service
        PageInfo<User> pageInfo = userService.queryPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        /*回显数据*/
        request.setAttribute("page",pageInfo);
        return "/WEB-INF/pages/system/user/list.jsp";
    }
    /**
     * 跳转到新增页面
     */

    public String toAdd(HttpServletRequest request,HttpServletResponse response) throws Exception{
        List<Dept> list = deptService.findAll();
        request.setAttribute("deptList",list);
        return "/WEB-INF/pages/system/user/add.jsp";
    }
    /**
     * 新增用户
     */
    public String save(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //获取表单数据
        User user = BeanUtil.fillBean(request, User.class, "yyyy-MM-dd");
        //调用Service
        userService.save(user);
        //跳转
        return "/system/user/list";
    }
    /**
     * 跳转到新增页面
     */
    public String toEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //获取表单数据
        String id = request.getParameter("id");
        /*查询祥细信息*/
        User user = userService.findById(id);
        List<Dept> list = deptService.findAll();
        /*回显数据*/
        request.setAttribute("user",user);
        request.setAttribute("deptList",list);
        return "/WEB-INF/pages/system/user/update.jsp";
    }
    /**
     * 编辑用户
     */
    public String edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //获取表单数据
        User user = BeanUtil.fillBean(request, User.class, "yyyy-MM-dd");
        //调用Service
        userService.update(user);
        //跳转
        return "/system/user/list";
    }
    /**
     * 跳转到授权页面
     */
    public String toRole(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //获取表单数据
        String id = request.getParameter("id");
        //查询详细信息
        User user = userService.findById(id);

        request.setAttribute("user",user);
        return "/WEB-INF/pages/system/user/author.jsp";
    }
    public String delete(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String[] ids = request.getParameterValues("id");
        userService.deleteById(ids);
        return "/system/user/list";
    }

}
